import { Button, Input, Checkbox, message } from 'antd'
import React, { useState } from 'react'
import '../index.less'
import axios from 'axios'
import {buka} from '../service'
const ReissueCard = ({ ItemData }) => {





  const [value, setValue] = useState(true);

  const [value2, setValue2] = useState(false);

  const [CardId, setCardId] = useState('')


  const ReadCard = () => {
    axios({
      url: "http://127.0.0.1:8989/datasnap/rest/TServerMethods1/reada",
      method: "post",
      headers: {
        "Content-Type": 'application/x-www-form-urlencoded;charset=utf-8',
        "Accept": "application/json"
      }
    }).then(res => {
      console.log(res)
      if (res.result == 0) {
        setCardId(res.cardid)
      } else {

      }

    }).catch(err => {

    })
  }

  const Cardbuka = () => {

    if (!CardId) return message.error("请先读卡")
    // 先建一个公共骨架
    const data = {
      cardid: CardId,
      guihao: ItemData.locker_number,
      lockclass: ItemData.locker_type,
      source: 1,
      uuid: Date.now().toString()
    };

    // 按需打标签
    if (value) data.autoruchang = 'on';
    if (value2) data.uncommoncard = 'on';

    buka(data).then(res => {
      if (res.result == 0) {
        message.success("补卡成功")
        setCardId('')
      } else {
        message.error("补卡失败 : " + res.msg)
      }
    }).catch(err => {
      message.error("补卡失败 : " + err.message)
    })
  }


  const onChange = e => {
    console.log(`checked = ${e.target.checked}`);
  };




  return (
    <div style={{ padding: "10px" }}>
      <div style={{ display: 'flex', alignItems: 'center' }}>
        <div style={{ margin: "0 10px", marginLeft: 20 }}><Button type='primary' onClick={() => ReadCard()}> 读卡</Button> : </div>
        <div><Input placeholder="手牌号" value={CardId} style={{ width: 290 }} /></div>
      </div>

      <div style={{ marginLeft: 100, marginTop: 16 }}>
        <Checkbox checked={value} onChange={(e) => setValue(e.target.checked)}>补卡后永久授权</Checkbox> <Checkbox checked={value2} onChange={(e) => setValue2(e.target.checked)}>非常规卡号</Checkbox>
      </div>

      <div className='btn bgc-green'
        style={{ marginLeft: 100, marginTop: 30, color: "#fff", textAlign: "center", padding: "0", width: 120 }}
        onClick={() => Cardbuka()}
      >
        补卡
      </div>
    </div>
  )
}

export default ReissueCard